package com.rabbit.eap.framework.common.controller;

import com.rabbit.eap.framework.common.enums.MsgEnum;
import com.rabbit.eap.framework.common.exception.EapException;
import com.rabbit.eap.framework.shiro.util.JwtUtil;
import com.rabbit.eap.framework.shiro.vo.LoginSysUserVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

/**
 * @projectName eap
 * @packageName com.rabbit.eap.framework.common.controller
 * @className BaseController
 * @author： rabbit
 * @version： 1.0
 * @since： 2020/4/21 16:47
 * <p>Description: BaseController
 * Controller父类
 * </p>

 */
@Slf4j
public class BaseController {
    /**
     * @Description 获取语言 主要为了国际化
     * @methodName: getLanguage
     * @param :
     * @return: java.lang.String
     * @date: 2020/6/2 19:28
     * @author: rabbit
     *
     */
    protected String getLanguage() {
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        if (requestAttributes != null) {
            return requestAttributes.getRequest().getHeader("Content-Language");
        }
        return null;
    }

    /**
     * @Description 根据token获取用户对象
     * @methodName: getUserFromWebToken
     * @param :
     * @return: com.rabbit.eap.framework.shiro.vo.LoginSysUserVo
     * @date: 2020/6/2 20:03
     * @author: rabbit
     *
     */
    public LoginSysUserVO getUserFromWebToken(){
        String token ;
        try {
            ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
            assert requestAttributes != null;
            token = requestAttributes.getRequest().getHeader("Authorization");
        }catch (Exception e){
            throw new EapException(MsgEnum.TOKEN_AUTHORIZATION_FAIL.getCode(), MsgEnum.TOKEN_AUTHORIZATION_FAIL.getDesc());
        }
        LoginSysUserVO loginSysUser = JwtUtil.getUser(token);

        return loginSysUser;
    }

}
